package com.ssy.wireless.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@Slf4j
public class WebApplicationListener implements HttpSessionListener, ServletContextListener {

    public WebApplicationListener() {
    }

    public void sessionCreated(HttpSessionEvent se) {
        if (log.isDebugEnabled()) {
            log.debug("Session [{}] created.", se.getSession().getId());
        }
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        if (log.isDebugEnabled()) {
            log.debug("Session [{}] destroyed.", se.getSession().getId());
        }
    }

    public void contextInitialized(ServletContextEvent event) {
        ServletContext sc = event.getServletContext();
        String path = sc.getContextPath();
        if (StringUtils.isEmpty(path) || path.equals("/")) {
            path = "";
        }
        sc.setAttribute("ctx", path);
        sc.setAttribute("assets", path + "/assets");
        log.info("Servlet context initialized.");
    }

    public void contextDestroyed(ServletContextEvent event) {
        log.info("Servlet context destroyed.");
    }
}
